using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.RepositoryFactory.SchemaDiscovery.ObjectModel.Base;
using Microsoft.Practices.RepositoryFactory.CodeGeneration.ObjectModel;
using Microsoft.Practices.RepositoryFactory.CustomPages.Helpers;
using Microsoft.Practices.RepositoryFactory.CodeGeneration.ObjectModel.Mappings;

namespace Microsoft.Practices.RepositoryFactory.CustomPages.CreateRepositoryPages
{
   public partial class MapResultSetPage : UserControl, IMapResultSetPage
   {
      MapResultSetPagePresenter presenter;

      public MapResultSetPage(MapResultSetPagePresenter presenter)
      {
         InitializeComponent();
         this.presenter = presenter;
         this.presenter.View = this;
      }

      public void FillEntityProperties(List<Property> properties)
      {

         List<Property> sortedProperties = new List<Property>(properties);
         sortedProperties.Sort();

         propertiesListbox.Items.Clear();

         propertiesListbox.DisplayMember = "Name";
         foreach (Property property in sortedProperties)
         {
            propertiesListbox.Items.Add(property);
         }
      }

      public void FillStoredProcedureOutputColumn(List<Parameter> parameters)
      {
         List<Parameter> sortedParameters = new List<Parameter>(parameters);
         sortedParameters.Sort();

         parametersListbox.Items.Clear();

         parametersListbox.DisplayMember = "Name";
         foreach (Parameter parameter in sortedParameters)
         {
            parametersListbox.Items.Add(parameter);
         }

      }

      public bool IsDataValid
      {
         get { return true; }
      }

      public void Complete()
      {
         presenter.OnComplete();
      }

      private void OnMapResultSetPageLoad(object sender, EventArgs e)
      {
         presenter.OnPageLoad();
      }

      private void addButton_Click(object sender, EventArgs e)
      {
         if (parametersListbox.SelectedIndex != -1 && propertiesListbox.SelectedIndex != -1)
         {
            Parameter parameter = (Parameter)parametersListbox.SelectedItem;
            Property property = (Property)propertiesListbox.SelectedItem;
            presenter.OnAddMapping(parameter, property);
         }
      }

      public void UpdateDropDowns(Parameter parameter, Property property, PropertyParameterMapping parameterMapping)
      {
         parametersListbox.Items.Remove(parameter);
         propertiesListbox.Items.Remove(property);

         listBox3.Items.Add(parameterMapping);
      }

      public void FillExistingMappings(List<PropertyParameterMapping> mappings)
      {
         List<PropertyParameterMapping> sortedMappings = new List<PropertyParameterMapping>(mappings);
         sortedMappings.Sort();

         listBox3.Items.Clear();
         foreach (PropertyParameterMapping mapping in sortedMappings)
         {
            listBox3.Items.Add(mapping);
         }
      }

      private void removeButton_Click(object sender, EventArgs e)
      {
         if (listBox3.SelectedIndex != -1)
         {
            PropertyParameterMapping propertyMapping = (PropertyParameterMapping)listBox3.SelectedItem;
            presenter.OnRemoveMapping(propertyMapping);
         }
      }
   }
}

